import time

from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from base import Base
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import random
import hashlib
from logger import Logger
from config import get_cfg


class Login(Base):
    """
    用户登录页面
    """
    logger = Logger('Login').get_log()
    loc_username = (By.NAME, "UserName")
    loc_password = (By.NAME, "PassWord")
    div = (By.CLASS_NAME, 'layui-layer-content')
    username = get_cfg('url', 'nsrsbh')
    password = get_cfg('url', 'password')

    def input_username(self, username):
        """输入用户名"""

        self.send_keys(self.loc_username, username)

    def input_password(self, password):
        """输入密码"""

        self.send_keys(self.loc_password, password)

    def captcha(self):
        """拖动滑块"""

        action_chains = ActionChains(self.driver)
        # 获取滑动按钮
        slide_bar_btn = self.driver.find_element_by_class_name('slideBarBtn')
        # 使用随机数确定滑动位置后滑动
        action_chains.drag_and_drop_by_offset(slide_bar_btn, random.randint(0, 180), 0).perform()

    def click_login(self):
        """登录"""

        locator = (By.XPATH, "//div[@class='loginBtn']")
        self.click(locator)

    # 定义统一登录入口
    def user_login(self, username=username, password=password):

        # 输入用户名、密码
        self.input_username(username)
        self.input_password(password)

        # 登录
        self.click_login()

        # 用户名或密码为空
        if username == '' or password == '':
            msg = self.get_attribute(self.div, 'textContent')
            return msg

        # 滑动
        # self.captcha()
        # mmp,这边只能显式等待，不然找不到元素

        # 用户名错误
        count = self.get_username(username)
        if count == 0:
            return self.get_attribute(self.div, 'textContent')

        # 密码错误
        m = hashlib.md5()
        b = password.encode(encoding='utf-8')
        m.update(b)
        password_md5 = m.hexdigest()
        pwd = self.get_password(username)
        if password_md5 != pwd:
            return self.get_attribute((By.XPATH, '//div[@class="layui-layer-content"]'), 'textContent')

        # 用户名密码正确 msg = 91131102570059848E
        msg = self.get_attribute((By.CLASS_NAME, 'userName'), 'textContent')
        return msg

    def get_username(self, username):
        """根据输入账号查询账号"""

        conn = self.get_conn()
        cursor = conn.cursor()
        with conn:
            cursor.execute('select count(*) from [User] t where t.Nsrsbh = ?', username)
            count = cursor.fetchone()[0]
        return count

    def get_password(self, username):
        """根据账号查询密码"""

        conn = self.get_conn()
        cursor = conn.cursor()
        with conn:
            cursor.execute('select t.PassWord from [User] t where t.Nsrsbh = ?', username)
            password = cursor.fetchone()[0]
        return password


if __name__ == '__main__':
    options = Options()
    options.add_argument('--headless')
    options.add_argument('--disable-gpu')
    driver = webdriver.Chrome(options=options)
    res = Login(driver).user_login(username='91131102570059848E', password='123456')
    print(res)
    driver.quit()
